package me.spring.cloud.common.components.log.aop;

import me.spring.cloud.common.components.log.model.TraceLog;
import me.spring.cloud.common.components.log.service.TraceLogService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * log处理器
 *
 * @author
 * @date 2018/9/11
 */
@Slf4j
@AllArgsConstructor
public class LogHandler {

  /**
   * 日志处理类
   */
  private TraceLogService logService;

  /**
   * 执行前处理
   *
   * @param className
   * @param methodName
   * @Return
   */
  public int preHandle(String className, String methodName) {
    return ConcurrentCounter.getConcurrent(className, methodName).incrementAndGet();
  }

  /**
   * 执行后处理
   *
   * @param inputParameters
   * @param className
   * @param methodName
   * @param startTime
   * @param output
   * @param t
   */
  public void postHandle(Object[] inputParameters, String className, String methodName, Long startTime,
      String providerAppId, Object output, Throwable t) {
    TraceLog ioLog = LogBuilder.buildLog(className, methodName, inputParameters, startTime, providerAppId, output, t);
    try {
      logService.saveTraceLog(ioLog);
    } catch (Exception e) {
      log.error(
          "aop save log error, service:" + ioLog.getClassName() + ",method=:" + ioLog.getMethod() + ",input=:" + ioLog
              .getInput(), e);
    }
    // 并发计数减值
    ConcurrentCounter.getConcurrent(className, methodName).decrementAndGet();
  }
}